from selenium import webdriver
from lxml import etree
import csv

file = open("ntent.csv", 'wt', newline='', encoding='utf-8')
writer = csv.writer(file)
def get_book():
    driver=webdriver.Chrome()                       #打开谷歌
    driver.get('http://m.dingdianxs.com/')        #输入网址
    driver.implicitly_wait(15)                   #等待
    account=driver.find_element_by_xpath('//*[@id="login_top"]/a[1]')  #定位登录
    account.click()
    user=driver.find_element_by_id('username')
    user.clear()
    user.send_keys('201806140057')                           #输入账号
    password=driver.find_element_by_id('userpass')
    password.clear()
    password.send_keys('33570638')                           #输入密码
    denglu=driver.find_element_by_xpath('/html/body/div[2]/div[4]/a')        #点击用户登录
    denglu.click()
    bookrack=driver.find_element_by_xpath('//*[@id="login_top"]/a[1]')         #点击书架
    bookrack.click()
    book=driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/h4/i/a')   #点击爬取的小说
    book.click()
    html = etree.HTML(driver.page_source, etree.HTMLParser())      #解析获取内容的网页
    return driver,html

def get_bookmessage(driver,html):
    writer.writerow(('书名','作者','类型', '状态', '最新更新时间', '最新章节', '简介'))
    book_name=html.xpath('/html/body/div[4]/div[1]/div[2]/h2/a/text()')  #书名
    author=html.xpath('/html/body/div[4]/div[1]/div[2]/p[3]/text()')    #作者
    type=html.xpath('/html/body/div[4]/div[1]/div[2]/p[4]/a/text()')     #类型
    status=html.xpath('/html/body/div[4]/div[1]/div[2]/p[5]/text()')    #状态
    time=html.xpath('/html/body/div[4]/div[1]/div[2]/p[6]/text()')     #最新更新时间
    new=html.xpath('/html/body/div[4]/div[1]/div[2]/p[7]/a/text()')    #最新章节
    intro=html.xpath('/html/body/div[4]/div[5]/text()')               #简介
    writer.writerow((book_name,author,type,status,time,new,intro))
    read = driver.find_element_by_xpath('/html/body/div[4]/div[3]/span[1]/a')    #点击开始阅读
    read.click()


def get_bookcontent():
    res = etree.HTML(driver.page_source, etree.HTMLParser())
    titles=res.xpath('//*[@id="nr1"]/text()[1]')     #标题内容
    title=(str(titles).replace('\\xa0',''))          #对标题内容进行修改
    contents=res.xpath('//*[@id="nr1"]')[0]
    contents1=contents.xpath('string(.)')          #含有br标签，提取文本内容
    content1=contents1.replace('\xa0','')             #对书本内容进行修正
    content2=content1.replace('\n','')                  #对书本内容进行修正
    content=content2.split('。')[1:]
    next=driver.find_element_by_xpath('//*[@id="pb_next"]')    #下一页与下一章的点击
    next.click()
    writer.writerow((title,content))           #写入csv

if __name__ == '__main__':
    driver,html=get_book()
    get_bookmessage(driver,html)
    writer.writerow(('标题', '文章内容'))
    for i in range(1500):
        get_bookcontent()      #for循环爬取


